package cn.iocoder.yudao.module.forward.controller.admin.scope;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.forward.controller.admin.scope.vo.*;
import cn.iocoder.yudao.module.forward.dal.dataobject.scope.ScopeDO;
import cn.iocoder.yudao.module.forward.dal.dataobject.scope.ScopeResourceDO;
import cn.iocoder.yudao.module.forward.service.scope.ScopeService;

@Tag(name = "管理后台 - 授权范围")
@RestController
@RequestMapping("/forward/scope")
@Validated
public class ScopeController {

    @Resource
    private ScopeService scopeService;

    @PostMapping("/create")
    @Operation(summary = "创建授权范围")
    @PreAuthorize("@ss.hasPermission('forward:scope:create')")
    public CommonResult<Integer> createScope(@Valid @RequestBody ScopeSaveReqVO createReqVO) {
        return success(scopeService.createScope(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新授权范围")
    @PreAuthorize("@ss.hasPermission('forward:scope:update')")
    public CommonResult<Boolean> updateScope(@Valid @RequestBody ScopeSaveReqVO updateReqVO) {
        scopeService.updateScope(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除授权范围")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('forward:scope:delete')")
    public CommonResult<Boolean> deleteScope(@RequestParam("id") Integer id) {
        scopeService.deleteScope(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得授权范围")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('forward:scope:query')")
    public CommonResult<ScopeRespVO> getScope(@RequestParam("id") Integer id) {
        ScopeDO scope = scopeService.getScope(id);
        return success(BeanUtils.toBean(scope, ScopeRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得授权范围分页")
    @PreAuthorize("@ss.hasPermission('forward:scope:query')")
    public CommonResult<PageResult<ScopeRespVO>> getScopePage(@Valid ScopePageReqVO pageReqVO) {
        PageResult<ScopeDO> pageResult = scopeService.getScopePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ScopeRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出授权范围 Excel")
    @PreAuthorize("@ss.hasPermission('forward:scope:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportScopeExcel(@Valid ScopePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ScopeDO> list = scopeService.getScopePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "授权范围.xls", "数据", ScopeRespVO.class,
                        BeanUtils.toBean(list, ScopeRespVO.class));
    }

    // ==================== 子表（授权范围信源关联） ====================

    @GetMapping("/scope-resource/list-by-scope-id")
    @Operation(summary = "获得授权范围信源关联列表")
    @Parameter(name = "scopeId", description = "授权范围")
    @PreAuthorize("@ss.hasPermission('forward:scope:query')")
    public CommonResult<List<ScopeResourceDO>> getScopeResourceListByScopeId(@RequestParam("scopeId") Integer scopeId) {
        return success(scopeService.getScopeResourceListByScopeId(scopeId));
    }

}